package com.qlu.syq.controller.admin;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.qlu.syq.entity.BookOrder;
import com.qlu.syq.entity.RoomType;
import com.qlu.syq.entity.admin.Checkin;
import com.qlu.syq.entity.admin.Room;
import com.qlu.syq.entity.admin.User;
import com.qlu.syq.enums.BookOrderStatus;
import com.qlu.syq.enums.RoomStatus;
import com.qlu.syq.enums.RoomTypeStatus;
import com.qlu.syq.page.admin.Page;
import com.qlu.syq.service.BookOrderService;
import com.qlu.syq.service.RoomTypeService;
import com.qlu.syq.service.admin.CheckinService;
import com.qlu.syq.service.admin.RoomService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;

/**
 * 入住管理后台控制器
 *
 * @author Administrator
 */
@RequestMapping("/admin/checkin")
@Controller
public class CheckinController {

    @Autowired
    private RoomService roomService;
    @Autowired
    private RoomTypeService roomTypeService;
    @Autowired
    private BookOrderService bookOrderService;
    @Autowired
    private CheckinService checkinService;

    /**
     * 入住管理列表页面
     *
     * @return
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public ModelAndView list(ModelAndView model) {
        model.addObject("roomTypeList", roomTypeService.findAll(new HashMap<>()));
        model.addObject("roomList", roomService.findAll(new HashMap<>()));
        model.setViewName("checkin/list");
        return model;
    }

    /**
     * 入住信息添加操作
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, String> add(Checkin checkin, @RequestParam(name = "bookOrderId", required = false) Long bookOrderId, HttpServletRequest request) {
        Map<String, String> ret = new HashMap<>();

        Long floorId = checkin.getFloorId();
        if (floorId == null) {
            User user = (User) request.getSession().getAttribute("admin");
            if (user.getFloorId() != null) {
                checkin.setFloorId(user.getFloorId());
            } else {
                ret.put("type", "error");
                ret.put("msg", "只允许各酒店负责人执行入住!");
                return ret;
            }
        }

        if (checkin == null) {
            ret.put("type", "error");
            ret.put("msg", "请填写正确的入住信息!");
            return ret;
        }
        if (checkin.getRoomId() == null) {
            ret.put("type", "error");
            ret.put("msg", "房间不能为空!");
            return ret;
        }
        if (checkin.getRoomTypeId() == null) {
            ret.put("type", "error");
            ret.put("msg", "房型不能为空!");
            return ret;
        }
        if (StringUtils.isEmpty(checkin.getName())) {
            ret.put("type", "error");
            ret.put("msg", "入住联系人名称不能为空!");
            return ret;
        }
        if (StringUtils.isEmpty(checkin.getMobile())) {
            ret.put("type", "error");
            ret.put("msg", "入住联系人手机号不能为空!");
            return ret;
        }
        if (StringUtils.isEmpty(checkin.getIdCard())) {
            ret.put("type", "error");
            ret.put("msg", "联系人身份证号不能为空!");
            return ret;
        }
        if (StringUtils.isEmpty(checkin.getArriveDate())) {
            ret.put("type", "error");
            ret.put("msg", "到达时间不能为空!");
            return ret;
        }
        if (StringUtils.isEmpty(checkin.getLeaveDate())) {
            ret.put("type", "error");
            ret.put("msg", "离店时间不能为空!");
            return ret;
        }
        // 入住
        try {
            checkinService.add(checkin, bookOrderId);
        } catch (Exception e) {
            ret.put("type", "error");
            ret.put("msg", "添加失败!");
        }

        ret.put("type", "success");
        ret.put("msg", "添加成功!");
        return ret;
    }

    /**
     * 入住信息编辑操作
     */
    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, String> edit(Checkin checkin, @RequestParam(name = "bookOrderId", required = false) Long bookOrderId) {
        Map<String, String> ret = new HashMap<>();
        if (checkin == null) {
            ret.put("type", "error");
            ret.put("msg", "请填写正确的入住信息!");
            return ret;
        }
        if (checkin.getRoomId() == null) {
            ret.put("type", "error");
            ret.put("msg", "房间不能为空!");
            return ret;
        }
        if (checkin.getRoomTypeId() == null) {
            ret.put("type", "error");
            ret.put("msg", "房型不能为空!");
            return ret;
        }
        if (StringUtils.isEmpty(checkin.getName())) {
            ret.put("type", "error");
            ret.put("msg", "入住联系人名称不能为空!");
            return ret;
        }
        if (StringUtils.isEmpty(checkin.getMobile())) {
            ret.put("type", "error");
            ret.put("msg", "入住联系人手机号不能为空!");
            return ret;
        }
        if (StringUtils.isEmpty(checkin.getIdCard())) {
            ret.put("type", "error");
            ret.put("msg", "联系人身份证号不能为空!");
            return ret;
        }
        if (StringUtils.isEmpty(checkin.getArriveDate())) {
            ret.put("type", "error");
            ret.put("msg", "到达时间不能为空!");
            return ret;
        }
        if (StringUtils.isEmpty(checkin.getLeaveDate())) {
            ret.put("type", "error");
            ret.put("msg", "离店时间不能为空!");
            return ret;
        }
        Checkin existCheckin = checkinService.find(checkin.getId());
        if (existCheckin == null) {
            ret.put("type", "error");
            ret.put("msg", "请选择正确的入住信息进行编辑!");
            return ret;
        }
        if (checkinService.edit(checkin) <= 0) {
            ret.put("type", "error");
            ret.put("msg", "编辑失败，请联系管理员!");
            return ret;
        }
        //编辑成功之后：1：判断房型是否发生变化，2：判断房间是否发生变化，3：判断是否是从预定订单来的信息
        //首先判断是否是从预定来的入住信息
        RoomType oldRoomType = roomTypeService.find(existCheckin.getRoomTypeId());
        RoomType newRoomType = roomTypeService.find(checkin.getRoomTypeId());

        //房型入住数不收预定订单的影响
        if (oldRoomType.getId().longValue() != newRoomType.getId().longValue()) {
            //说明房型发生了变化，原来的房型入住数恢复，新的房型入住数增加
            oldRoomType.setLivedNum(oldRoomType.getLivedNum() - 1);
            newRoomType.setLivedNum(newRoomType.getLivedNum() + 1);
            if (bookOrderId == null) {
                oldRoomType.setAvilableNum(oldRoomType.getAvilableNum() + 1);
                newRoomType.setAvilableNum(newRoomType.getAvilableNum() - 1);
            }
        }
        /**
         if(bookOrderId == null){
         //表示不是从预定订单来的，此时需判断原来的入住信息是否来源于预定
         if(existCheckin.getBookOrderId() == null){
         oldRoomType.setAvilableNum(oldRoomType.getAvilableNum() + 1);
         newRoomType.setAvilableNum(newRoomType.getAvilableNum() - 1);
         }
         if(existCheckin.getBookOrderId() != null){
         //表示原来的入住信息来源于预定，但是新的入住信息不是来源于预定,需恢复原来的预定状态
         BookOrder oldBookOrder = bookOrderService.find(existCheckin.getBookOrderId());
         oldBookOrder.setStatus(0);
         bookOrderService.edit(oldBookOrder);
         oldRoomType.setBookNum(oldRoomType.getBookNum() + 1);
         }
         }
         //表示此时的订单是来源于预定
         if(bookOrderId != null){
         //表示是从预定订单来的，此时需判断原来的入住信息是否来源于预定
         if(existCheckin.getBookOrderId() != null){
         //表示原来的入住信息来源于预定，但是新的入住信息不是来源于预定,需恢复原来的预定状态
         BookOrder oldBookOrder = bookOrderService.find(existCheckin.getBookOrderId());
         if(bookOrderId.longValue() != oldBookOrder.getId().longValue()){
         oldBookOrder.setStatus(0);
         bookOrderService.edit(oldBookOrder);
         //oldRoomType.setBookNum(oldRoomType.getBookNum() + 1);
         }
         }
         if(oldRoomType.getId().longValue() != newRoomType.getId().longValue()){
         newRoomType.setBookNum(newRoomType.getBookNum() - 1);

         if(existCheckin.getBookOrderId() == null){
         oldRoomType.setAvilableNum(oldRoomType.getAvilableNum() + 1);
         }else{
         oldRoomType.setBookNum(oldRoomType.getBookNum() + 1);
         }
         }
         BookOrder newBookOrder = bookOrderService.find(bookOrderId);
         newBookOrder.setStatus(1);
         bookOrderService.edit(newBookOrder);
         }**/
        roomTypeService.updateNum(newRoomType);
        roomTypeService.updateNum(oldRoomType);
        //判断房间是否发生变化
        if (checkin.getRoomId().longValue() != existCheckin.getRoomId().longValue()) {
            //表示房间发生了变化
            Room oldRoom = roomService.find(existCheckin.getRoomId());
            Room newRoom = roomService.find(checkin.getRoomId());
            oldRoom.setStatus(0);//原来的房间可入住
            newRoom.setStatus(1);//现在的房间已入住
            roomService.edit(newRoom);
            roomService.edit(oldRoom);
        }
        ret.put("type", "success");
        ret.put("msg", "修改成功!");
        return ret;
    }

    /**
     * 分页查询入住信息
     *
     * @return
     */
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> list(
            @RequestParam(name = "name", defaultValue = "") String name,
            @RequestParam(name = "roomId", defaultValue = "") Long roomId,
            @RequestParam(name = "roomTypeId", defaultValue = "") Long roomTypeId,
            @RequestParam(name = "idCard", defaultValue = "") String idCard,
            @RequestParam(name = "mobile", defaultValue = "") String mobile,
            @RequestParam(name = "status", required = false) Integer status,
            Page page) {
        Map<String, Object> ret = new HashMap<>();
        Map<String, Object> queryMap = new HashMap<>();
        queryMap.put("name", name);
        queryMap.put("status", status);
        queryMap.put("roomId", roomId);
        queryMap.put("roomTypeId", roomTypeId);
        queryMap.put("idCard", idCard);
        queryMap.put("mobile", mobile);
        queryMap.put("offset", page.getOffset());
        queryMap.put("pageSize", page.getRows());
        ret.put("rows", checkinService.findList(queryMap));
        ret.put("total", checkinService.getTotal(queryMap));
        return ret;
    }

    /**
     * 退房操作
     *
     * @param checkId
     */
    @RequestMapping(value = "/checkout", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, String> checkout(Long checkId) {
        Map<String, String> ret = new HashMap<>();
        if (checkId == null) {
            ret.put("type", "error");
            ret.put("msg", "请选择数据!");
            return ret;
        }

        try {
            checkinService.checkout(checkId);
        } catch (Exception e) {
            ret.put("type", "error");
            ret.put("msg", "退房失败，请联系管理员!");
            return ret;
        }

        ret.put("type", "success");
        ret.put("msg", "退房成功!");
        return ret;
    }

    /**
     * 根据房间类型获取房间
     *
     * @return
     */
    @RequestMapping(value = "/load_room_list", method = RequestMethod.POST)
    @ResponseBody
    public List<Map<String, Object>> load_room_list(Long roomTypeId) {
        List<Map<String, Object>> retList = new ArrayList<>();
        Map<String, Object> queryMap = new HashMap<>();
        queryMap.put("roomTypeId", roomTypeId);
        queryMap.put("status", 0);
        queryMap.put("offset", 0);
        queryMap.put("pageSize", 999);
        List<Room> roomList = roomService.findList(queryMap);
        for (Room room : roomList) {
            Map<String, Object> option = new HashMap<>();
            option.put("value", room.getId());
            option.put("text", room.getSn());
            retList.add(option);
        }
        return retList;
    }
}
